package com.xiao.live.service.impl;

import com.xiao.group.common.Vo.GoodsEntity;
import com.xiao.group.common.Vo.UsersEntity;
import com.xiao.group.common.utils.R;
import com.xiao.live.config.AliPlayAndPushUtils;
import com.xiao.live.entity.LiveStreamingEntity;
import com.xiao.live.feigngoods.FeignGoods;
import com.xiao.live.feigngoods.UserFeign;
import com.xiao.live.service.LiveStreamingService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiao.group.common.utils.PageUtils;
import com.xiao.group.common.utils.Query;

import com.xiao.live.dao.LiveActicityDao;
import com.xiao.live.entity.LiveActicityEntity;
import com.xiao.live.service.LiveActicityService;
import sun.java2d.pipe.SolidTextRenderer;


@Service("liveActicityService")
public class LiveActicityServiceImpl extends ServiceImpl<LiveActicityDao, LiveActicityEntity> implements LiveActicityService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<LiveActicityEntity> page = this.page(
                new Query<LiveActicityEntity>().getPage(params),
                new QueryWrapper<LiveActicityEntity>()
        );

        return new PageUtils(page);
    }

    @Autowired
    FeignGoods feignGoods;
    @Override
    public R togoodsList(Integer liveId) {
        LiveActicityEntity acticityEntity = this.getById(liveId);
        String goodsid = acticityEntity.getGoodsid();
        String[] split = goodsid.split(",");
        ArrayList<GoodsEntity> list = new ArrayList<>();

        for (String s : split) {
            GoodsEntity toinfo = feignGoods.toinfo(Integer.parseInt(s));
            list.add(toinfo);
        }
        return R.ok().put("data",list);
    }

    @Override
    public R toliveLists(Integer liveId) {
        LiveActicityEntity acticityEntity = this.getById(liveId);
        if(StringUtils.isNotBlank(acticityEntity.getGoodsid())){
            List<GoodsEntity> goodsEntities = feignGoods.togoodsList(acticityEntity.getGoodsid());
            return R.ok().put("data",goodsEntities);
        }
        List<GoodsEntity> togoodslists = feignGoods.togoodslists();
        return R.ok().put("data",togoodslists);
    }

    @Autowired
    LiveStreamingService liveStreamingService;
    //直播商品的添加
    @Override
    public R toaddlist(Integer liveId, Integer goodsId) {
        LiveStreamingEntity liveStreamingEntity = new LiveStreamingEntity();
        liveStreamingEntity.setGoodsId(goodsId);
        liveStreamingEntity.setLiveId(liveId);
        //添加中间表中的数据
        boolean save = liveStreamingService.save(liveStreamingEntity);

        LiveActicityEntity liveActicityEntity = this.getById(liveId);
        if(liveActicityEntity.getGoodsid().equals("")){
            liveActicityEntity.setGoodsid(goodsId.toString());
        }else {
            liveActicityEntity.setGoodsid(liveActicityEntity.getGoodsid()+","+goodsId);
        }

        int i = baseMapper.updateById(liveActicityEntity);
        return R.ok().put("data",save).put("addstatus",i);
    }

    @Override
    public R toxia(Integer liveId, Integer goodsId) {
        LiveActicityEntity liveActicityEntity = this.getById(liveId);
        LiveStreamingEntity liveStreamingServiceOne = liveStreamingService.getbyidlivelist(liveId,goodsId);
        liveStreamingService.removeById(liveStreamingServiceOne.getMid());

        List<LiveStreamingEntity> liveStreamingEntities = liveStreamingService.list(new QueryWrapper<LiveStreamingEntity>().eq("live_id",liveId));
        String attr  = "";
        for (LiveStreamingEntity liveStreamingEntity : liveStreamingEntities) {
            attr = attr+ liveStreamingEntity.getGoodsId()+",";
        }
        String substring = attr.substring(0, attr.length()-1);
        liveActicityEntity.setGoodsid(substring);
        int i = baseMapper.updateById(liveActicityEntity);
        return R.ok().put("data",i);
    }

    @Autowired
    AliPlayAndPushUtils aliPlayAndPushUtils;
    @Autowired
    UserFeign userFeign;

    @Override
    public R tosave(LiveActicityEntity liveActicity) {
        UsersEntity usersEntity = userFeign.info(liveActicity.getUserid());
        liveActicity.setUsername(usersEntity.getUsername());
        liveActicity.setLiveStatus(0);

        int insert = baseMapper.insert(liveActicity);
        return R.ok().put("data",insert);
    }

    @Override
    public R tokaibo(Integer liveId) {
        LiveActicityEntity liveActicityEntity = this.getById(liveId);
        String playUrl = aliPlayAndPushUtils.toplayUrl();
        String pushUrl = aliPlayAndPushUtils.topushUrl();
        //
        liveActicityEntity.setUrls(playUrl);
        //
        liveActicityEntity.setUrlss(pushUrl);
        boolean b = this.updateById(liveActicityEntity);
        return R.ok().put("data",b);
    }

}